//
//  AchievementDetailVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/1/10.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "AchievementDetailVC.h"
#import "AchievementModel.h"
#import "AchievementDetailCell.h"
#import "AchievementStore.h"

@interface AchievementDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) ViewHeader *header;
@property (nonatomic, strong) UITableView *TableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation AchievementDetailVC

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBarWithTitle:@"业绩详情"];
    [self initCSS];
    [self loadData];
}
- (void)initCSS{
    WS(weakself)
    self.header = [[ViewHeader alloc]initWithAchievementDetailHeader];
    self.header.AchieveTimelab.text = [NSString stringWithFormat:@"%@年%@月%@日",[self.muttime substringWithRange:NSMakeRange(0, 4)],[self.muttime substringWithRange:NSMakeRange(5, 2)],[self.muttime substringWithRange:NSMakeRange(8, 2)]];
    [self.header setAchievetoTimeBlock:^{
        [weakself ChooseTime];
    }];
    [self.header setAchieveTimeSearchBlock:^{
        if (weakself.dataArr.count > 0) {
            [weakself.dataArr removeAllObjects];
        }
        [weakself loadData];
    }];
    [self.view addSubview:self.header];
    [self.header mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(45);
        
    }];
    
    self.TableView = [[UITableView alloc]initWithFrame:CGRectMake(0, iPhoneX_Top, ScreenWidth, ScreenHeight - (iPhoneX_Top)) style:UITableViewStyleGrouped];
    [self.TableView registerNib:[UINib nibWithNibName:@"AchievementDetailCell" bundle:nil] forCellReuseIdentifier:@"AchievementDetailCellID"];
    self.TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.TableView.showsVerticalScrollIndicator = NO;
    self.TableView.delegate = self;
    self.TableView.dataSource = self;
    [self.view insertSubview:self.TableView belowSubview:self.header];
    [self.TableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.header.mas_bottom);
    }];
}
- (void)ChooseTime{
    NSDate *minDate = [NSDate setYear:1978 month:3 day:12];
    NSDate *maxDate = [NSDate date];
    [BRDatePickerView showDatePickerWithTitle:@"选择日期" dateType:BRDatePickerModeYMD defaultSelValue:self.muttime minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
        NSArray *listArr = [selectValue componentsSeparatedByString:@"-"];
        self.header.AchieveTimelab.text = [NSString stringWithFormat:@"%@年%@月%@日",listArr[0],listArr[1],listArr[2]];
        self.muttime = [NSString stringWithFormat:@"%@-%@-%@",listArr[0],listArr[1],listArr[2]];
    } cancelBlock:^{
        
        
    }];
}
- (void)loadData{
    [[[AchievementStore alloc]init] GetAchievementDetailWithPrams:@{@"customId":[UserDefaultsManager sharedInstance].user_id,@"strDate":self.muttime} Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            NSArray *arr = res[@"data"][@"detailList"];
            if (arr.count > 0) {
                for (NSDictionary *dic in arr) {
                    AchievementDetailModel *model = [[AchievementDetailModel alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.dataArr addObject:model];
                }
            }
        }
        [self.TableView reloadData];
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 413;
    AchievementDetailModel *model = self.dataArr[indexPath.section];
    if (model.isFrist) {
        height += 91;
    }
    if (model.isSec) {
        height += 91;
    }
    if (model.isThird) {
        height += 213;
    }
    if (model.isFourth) {
        height += 227;
    }
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 32;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 32)];
    header.backgroundColor = [UIColor colorWithHexString:@"f1f1f1"];
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(14, 10, SCREEN_WIDTH - 30, 12)];
    title.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
    title.textColor = [UIColor colorWithHexString:@"666666"];
    if (self.dataArr.count > 0) {
        AchievementDetailModel *model = self.dataArr[section];
        title.text = model.date;
    }
    [header addSubview:title];
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AchievementDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AchievementDetailCellID"];
    if (self.dataArr.count > 0) {
        AchievementDetailModel *model = self.dataArr[indexPath.section];
        cell.model = model;
    }
    [cell setSendIsFristBlock:^(BOOL frist) {
        AchievementDetailModel *model1 = self.dataArr[indexPath.section];
        model1.isFrist = frist;
        [tableView reloadData];
    }];
    [cell setSendIsSecBlock:^(BOOL sec) {
        AchievementDetailModel *model2 = self.dataArr[indexPath.section];
        model2.isSec = sec;
        [tableView reloadData];
    }];
    [cell setSendIsThirdBlock:^(BOOL third) {
        AchievementDetailModel *model3 = self.dataArr[indexPath.section];
        model3.isThird = third;
        [tableView reloadData];
    }];
    [cell setSendIsFourthBlock:^(BOOL fourth) {
        AchievementDetailModel *model4 = self.dataArr[indexPath.section];
        model4.isFourth = fourth;
        [tableView reloadData];
    }];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
